﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using SdmxMl.GenericData;

namespace SdmxMl.UC
{
    public partial class UCSeries : UserControl
    {
        private BindingManagerBase bm = null;
        private GroupSerieAttrib gsa;
        private DataSetType _dataset = null;
        private SeriesTypeList _seriesList = null;
        private bool inBinding;

        public UCSeries()
        {
            InitializeComponent();
            gsa = new GroupSerieAttrib();
        }

        public void SetSeries(DataSetType dataset, SeriesTypeList seriesList)
        {
            _dataset = dataset;
            _seriesList = seriesList;
            DataTable dtSeries = gsa.ConstructSeriesDataTable(_dataset, _seriesList);

   //         this.dataGridViewSeries.BeginUpdate();
            inBinding = true;
            this.dataGridViewSeries.Columns.Clear();
            this.dataGridViewSeries.DataSource = dtSeries;
            foreach (DataGridViewColumn ch in dataGridViewSeries.Columns)
                if (ch.Width < 90)
                    ch.Width = 90;
            inBinding = false;

  //          this.dataGridViewSeries.EndUpdate();
  //          dataGridViewSeries.SelectedIndex = 0;
            dataListViewSerie_SelectedIndexChanged(this, EventArgs.Empty);

        }

        private void dataListViewSerie_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (inBinding)
                return;

            bm = dataGridViewSeries.BindingContext[dataGridViewSeries.DataSource, dataGridViewSeries.DataMember];
            if (bm.Count > 0)
            {
                DataRow findRow = ((DataRowView)bm.Current).Row;
                if (findRow != null)
                {
                    int index = (dataGridViewSeries.DataSource as DataTable).Rows.IndexOf(findRow);

                    if (index >= 0)
                    {
                        DataTable t = gsa.ConstructObjDataTable(_dataset, _seriesList[index].ObsList);
                        if (t != null)
                        {
                            //    this.dataListViewObs.BeginUpdate();
                            this.dataGridViewObs.Columns.Clear();
                            this.dataGridViewObs.DataSource = t;
                            foreach (DataGridViewColumn ch in dataGridViewObs.Columns)
                                if (ch.Width < 90)
                                    ch.Width = 90;
                            //      this.dataListViewObs.EndUpdate();
                        }
                    }
                }
            }

        }
    }
}
